import payKeyboard from "components/payKeyboard";
import { payKeyboardAsync } from "utils/payKeyboard";
import diyBackGround from "components/diyBackGround";

export default {
  data() {
    return {
      hideCNY: false,//是否显示货币符号
      pageTitle: '页面加载',
      pageOptions: {},
      checkInfo: {},
      showTRMember: true,
      showBindMember: false,
      bindMemberStatus: false,

      info_form: {
        transfer_money: ""
      },
      transfer_id: "",
      memberInfo: "",
      memberlabel: '',
      disabledTar: false,
      //余额字样

      payKeyboardShow: false, //键盘显示
      need_password: false, //是否需要验证支付密码
      has_password: false, //是否设置了支付密码

      showConfirm: false, //确认转账弹窗
      transferRatioVal: '--',

      transferTargetVal: ''
    };
  },
  watch: {
    transfer_id: function(new_transfer_id) {
      $http
        .get("member.member.memberInfo",{uid: this.transfer_id},"获取用户中").then(
          (response)=> {
            if (response.result === 1) {
              this.memberInfo = response.data;
              this.memberlabel = `昵称：${response.data.nickname || '--'} 姓名：${response.data.realname || '--'}`;
            } else {
              this.memberlabel = response.msg;
              this.$toast(response.msg);
            }
          },
          function(response) {
            this.$oast(response.msg);
          }
        );
    },
    'info_form.transfer_money':function(newV,oldV){
      if(this.pageOptions.transferRatio) {
        //开启了转让比例
        let transfer_money = Number(this.info_form.transfer_money);
        let _tra = transfer_money * this.pageOptions.transferRatio.PCT / 100;
        this.transferRatioVal = (transfer_money + _tra).toFixed(2);
      }
    }
  },
  methods: {
    beforeConfirm() {
      //确认转账前处理
      if (parseFloat(this.info_form.transfer_money) > parseFloat(this.checkInfo.credit)) {
        this.$dialog.alert({ message: `${this.pageOptions.transferText}数额不可大于您的${this.pageOptions.numText}` });
        return;
      }
      if (this.showTRMember && (this.transfer_id == undefined || this.transfer_id <= 0 || this.transfer_id.length == 0)) {
        this.$dialog.alert({ message: `${this.pageOptions.transferName}id不可为空` });
        return;
      }
      if (this.showBindMember && !this.checkInfo.third_uid) {
        this.$dialog.alert({ message: `${this.pageOptions.showBindMemberOption.name}不能为空` });
        return;
      }
      if (this.info_form.transfer_money == undefined || this.info_form.transfer_money <= 0 || this.info_form.length == 0) {
        this.$dialog.alert({ message: `${this.pageOptions.transferText}数额不可低于0元` });
        return;
      }
      if(this.disabledTar) {
        this.$dialog.alert({ message: `请先确认${this.pageOptions.transferTarget.name}` });
        return;
      }

      if (this.need_password == true && this.has_password == false) {
        this.$dialog.confirm({ message: "请先设置支付密码" }).then(() => {
          this.$router.push(this.fun.getUrl("set_balance_password", {}));
        });
        return;
      }
      this.showConfirm = true;
    },
    //确认转账
    async confirm() {
      if(this.lockClick) return;//防止重复点击
      this.lockClick = true;
      let json = {};
      if(this.pageOptions.submitKV.uid) json[this.pageOptions.submitKV.uid] = this.transfer_id;
      if(this.pageOptions.submitKV.num) json[this.pageOptions.submitKV.num] = this.info_form.transfer_money;

      if(this.pageOptions.transferTarget) {
        json[this.pageOptions.transferTarget.submitKey] = this.transferTargetVal;
      }
      if (this.need_password == true) {
        //开启支付密码验证
        let pass = await this.getPayKeyboardPassword();
        json.password = pass;
      }
      $http.get(this.pageOptions.transferApi, json, " ").then((response)=> {
        if (response.result === 1) {
          this.$dialog.alert({ message: response.msg }).then(() => {
            this.lockClick = false;
            this.$router.go(-1);
          });
        } else {
          this.lockClick = false;
          this.$dialog.alert({ message: response.msg });
        }
      },
      (response)=> {
        // error callback
      });
    },

    payComplete(text) {
      console.log(text, "页面");
      payKeyboardAsync.complete(text);
    },
    getPayKeyboardPassword() {
      this.payKeyboardShow = true;
      return new Promise((resove, reject) => {
        payKeyboardAsync.addCompleteFn(pass => {
          resove(pass);
        });
      });
      // let pass = await this.getPayKeyboardPassword();
    },
    async bindMemberFn() {
      // 绑定会员id
      let { result, msg, data } = await $http.get(this.pageOptions.showBindMemberOption.bindApi,{
        third_uid: this.checkInfo.third_uid
      },".");
      this.$toast(msg);
      this.getData();
    },
    async unbindMemberFn() {
      // 解绑会员
      let { result, msg, data } = await $http.get(this.pageOptions.showBindMemberOption.unbindApi,{},".");
      this.$toast(msg);
      this.getData();
    },
    async getData() {
      let { result, msg, data } = await $http.get(this.pageOptions.checkNumApi,{},".");
      if (result == 0) return this.$toast(msg);
      if (!data) return this.$toast('返回的数据为空！');
      this.setPageData(data);
    },
    setPageData(data) {
      if(typeof this.pageOptions.numText !== 'function') {
        this.pageOptions.numText = data.plugin_name || data.page_title;
        this.pageTitle = this.pageOptions.transferText + this.pageOptions.numText;
      } else {
        this.pageOptions.numText = this.pageOptions.numText();
        this.pageTitle = data.plugin_name || data.page_title;
      }

      this.fun.setWXTitle(this.pageTitle);
      this.checkInfo = data;
      this.bindMemberStatus = this.checkInfo.third_uid ? true : false;

      if(this.pageOptions.transferTarget) {
        this.disabledTar = true;
      }
      // this.need_password = data.need_password;
      // this.has_password = data.has_password;
    },
    async checkTarget(){
      if(!this.transferTargetVal) {
        this.$dialog.alert({ message: `请填写${this.pageOptions.transferTarget.name}` });
        return;
      }
      let json = {};
      json[this.pageOptions.transferTarget.serchKey] = this.transferTargetVal;
      let { result, msg, data } = await $http.get(this.pageOptions.transferTarget.api,json,".");
      this.$toast(msg);
      if (result == 0) return;
      this.disabledTar = false;
    },
    initData(){
      if(this.pageOptions.hiddenTRMember) this.showTRMember = false;
      if(this.pageOptions.showBindMember) this.showBindMember = true;
      if(this.pageOptions.hideCNY) this.hideCNY = true;
    },
    async initConfig() {
      let { result, msg, data } = await $http.get(this.$route.query.api,{},".");
      if (result == 0) return this.$toast(msg);
      this.pageOptions = data||{};
      this.initData();
      this.setPageData(data);
    }
  },
  computed:{},
  created() {
    this.pageOptionsLis = {
      'agency-balance': {
        checkNumApi: 'plugin.agency-balance.frontend.operate-balance.info',
        transferApi: 'plugin.agency-balance.frontend.operate-balance.transfer',
        numText: '--',
        transferName: '受让人',
        transferText: '转账',
        submitKV: {
          uid: 'transfer_id',
          num: 'balance'
        }
      },
      'store-balance-transfer': {
        checkNumApi: 'plugin.store-balance.frontend.operate-balance.info',
        transferApi: 'plugin.store-balance.frontend.operate-balance.transfer',
        numText: '--',
        transferName: '受让人',
        transferText: '转让',
        submitKV: {
          uid: 'recipient_id',
          num: 'trade_value'
        }
      },
      "ycc-money":{
        checkNumApi: 'plugin.ycc-money.frontend.move.index',
        transferApi: 'plugin.ycc-money.frontend.move.move',
        numText: '--',
        transferName: '受让人',
        transferText: '转让',
        submitKV: {
          uid: 'move_uid',
          num: 'amount'
        }
      },
      "acc-points":{
        checkNumApi: 'plugin.acc-points.frontend.records.dash',
        transferApi: 'plugin.acc-points.frontend.records.transfer',
        hiddenTRMember: true,//隐藏受让人输入框
        showBindMember: true,
        hideCNY: true,//隐藏货币符号
        showBindMemberOption: {
          name: '绑定uid',
          bindApi: 'plugin.acc-points.frontend.records.bind',
          unbindApi: 'plugin.acc-points.frontend.records.unbind'
        },
        numText: (()=>{
          return this.fun.getCustomTextLang('member_center.credit1','积分');
        }),
        transferName: '绑定uid',
        transferText: '划转',
        submitKV: {
          num: 'number'
        }
      },
      'digitalID_available': {
        checkNumApi: '获取数据接口',
        transferApi: '提交转让接口',
        transferName: '受让人',
        transferText: '转让',
        submitKV: {
          uid: '提交转让接口的提交参数名，对应受让人id',
          num: '提交转让接口的提交参数名,对应转让的数量'
        },
        transferRatio: {
          PCT: 10,//百分比
          PCTDesc: '转让手续费比例',
          inputDesc: '实际扣除数量',
          actualDesc: '到账数量',
        }
      },
      'digitalID_unavailable': {
        checkNumApi: '获取数据接口',
        transferApi: '提交转让接口',
        hiddenTRMember: true,//隐藏受让人输入框
        transferText: '转让',
        submitKV: {
          num: '提交转让接口的提交参数名,对应转让的数量'
        },
        transferRatio: {
          PCT: 10,//百分比
          PCTDesc: '转让手续费比例',
          inputDesc: '实际扣除数量',
          actualDesc: '到账数量',
        },
        transferTarget: {
          name: '手机号',
          submitKey: '提交转让接口的提交参数名，例如：mobile,对应上方输入框的值，本次对应手机号',
        }
      }
    };
    let plu = this.$route.query.plu;
    this.pageOptions = this.pageOptionsLis[plu];

    if (this.$route.query.api) {
      this.initConfig();
    } else if(plu){
      this.initData();
      this.getData();
    } else {
      this.$toast('缺少必要参数');
    }

  },
  components: { payKeyboard, diyBackGround }
};
